// 查询账户绑定的其他账户
import express from "express";
import pool from "../importantApi/db_connection.js";

const router = express.Router();
router.get("/", async (req, res) => {
    const {userId} = req.query;
    if (!userId) {
        return res.status(401).json({code: 401, msg: "缺少必要参数"});
    }
    try {
        let [result] = await pool.query(`
            select u.user_id,
                   u.user_name,
                   u.identity
            from userinfo u
                     join (select to_user_id as related_id
                           from user_relationship
                           where from_user_id = ${userId}
                           union
                           select from_user_id as related_id
                           from user_relationship
                           where to_user_id = ${userId}) as combined
                          on u.user_id = combined.related_id
            where u.user_id != ${userId}
        `)
        return res.status(200).json({
            code: 200,
            data: result
        })
    } catch (err) {
        console.log(err);
        return res.status(500).json({
            code: 500,
            msg: "关联查询错误：server error"
        })
    }
})
export default router;